package com.ruoyi.syrw.aspectj;


import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.report_tg.domain.bo.ExcelGeneralBo;
import com.ruoyi.syrw.domain.BsDataReport;
import com.ruoyi.syrw.domain.RefReportExcel;
import com.ruoyi.syrw.domain.vo.RefReportExcelVo;
import com.ruoyi.syrw.mapper.BiExcelTypeMapper;
import com.ruoyi.syrw.mapper.BsDataReportMapper;
import com.ruoyi.syrw.mapper.RefReportExcelMapper;
import com.ruoyi.syrw.service.IBsDataReportService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.ycl.mapper.BsJctzMapper;
import com.ruoyi.yp.mapper.BsYpMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
@Order(100)
public class ComputeReportResultAspect {

    private final RefReportExcelMapper refReportExcelMapper;
    private final BsDataReportMapper reportMapper;
    private final IBsDataReportService reportService;
    private final BsYpMapper ypMapper;
    private final ISysDictDataService dictDataService;
    private final ISysDeptService deptService;
    private final BsJctzMapper jctzMapper;
    private final BiExcelTypeMapper excelTypeMapper;

    /**
     * 填充字段
     * 处理完请求后执行
     * 对于report 1：合格 2：不合格；  0：初始
     * @param joinPoint 切点
     */
    @Around(value = "@annotation(computeReportResult)")
    public void fillResult(ProceedingJoinPoint joinPoint, ComputeReportResult computeReportResult) throws Throwable {

        joinPoint.proceed();

        if (ObjUtil.isEmpty(joinPoint.getArgs()) || ObjUtil.isEmpty(joinPoint.getArgs()[0])) {
            throw new ServiceException("注解使用有误，或者被注解的方法不规范，请联系管理员解决。错误详细：" + joinPoint.getSourceLocation().toString());
        }
        ExcelGeneralBo bo = (ExcelGeneralBo) joinPoint.getArgs()[0];
        RefReportExcelVo ref = refReportExcelMapper.selectVoOne(new LambdaQueryWrapper<RefReportExcel>()
            .eq(RefReportExcel::getExcelId, bo.getExcelId()));

//            .stream().map(RefReportExcelVo::getExcelId).collect(Collectors.toList());
        if (2 == bo.getExcelResult()) {
            // 直接给报告赋值为不合格
            setBadResult(ref.getReportId(), 2);
        } else {
            // 判断该任务下 其他报告 是否均为 合格
            List<RefReportExcelVo> refReportExcelVos = refReportExcelMapper.selectVoList(new LambdaQueryWrapper<RefReportExcel>()
                .eq(RefReportExcel::getReportId, ref.getReportId())
                .ne(RefReportExcel::getExcelId, bo.getExcelId()));
            if (reportService.isAllOk(refReportExcelVos)) {
                setBadResult(ref.getReportId(), 1);
            } else {
                setBadResult(ref.getReportId(), 2);
            }

        }

    }

    private void setBadResult(Long reportId, Integer result) {
        reportMapper.update(null, new LambdaUpdateWrapper<BsDataReport>()
            .set(BsDataReport::getReportResult, result)
            .eq(BsDataReport::getReportId, reportId));
    }
}
